<?php
/*
 * 导入处理
 * @package    vendor
 * @author     Mark
 * @copyright  Copyright (c) 2013 ixdcw team
 * @version    $Id: f2d57896b175f6e510e0ad084345e28ac500980c $
 */

/**
 * 循环导入
 * Class ImportLoop
 */
class ImportLoop {

	/**
	 * 链接
	 * @var
	 */
	private $_source;
	private $_aim;

	/**
	 * 数据链接
	 * @var
	 */
	private $_dbSource;
	private $_dbAim;

	/**
	 * 源表和源key
	 * @var
	 */
	private $_sourceTable;
	private $_sourceKey;
	private $_sourceCondition;

	private $_params = array();

	/**
	 *
	 */
	public function __construct() {

	}

	/**
	 * 'mysql:host=192.168.1.19;dbname=caiji', 'root', 'root'
	 * @param $link
	 * @param $username
	 * @param $pass
	 */
	public function setSource($link, $username, $pass) {
		$this->_source   = array($link, $username, $pass);
		try {
			$this->_dbSource = new PDO($link, $username, $pass);
			$this->_dbSource->query('set names utf8');
		} catch (PDOException $exception) {
			var_dump( $exception->getMessage());
			exit;
		}
	}

	/**
	 * @param $table
	 * @param $checkKey
	 * @param $condition
	 */
	public function setSourceInfo($table, $checkKey, $condition) {
		$this->_sourceTable     = $table;
		$this->_sourceKey       = $checkKey;
		$this->_sourceCondition = $condition;
	}

	/**
	 * 获取源表
	 * @return mixed
	 */
	public function getSourceTable() {
		return $this->_sourceTable;
	}

	/**
	 * @param $link
	 * @param $username
	 * @param $pass
	 */
	public function setAim($link, $username, $pass) {
		$this->_aim   = array($link, $username, $pass);
		try {
			$this->_dbAim = new PDO($link, $username, $pass);
			$this->_dbAim->query('set names utf8');
		} catch (PDOException $exception) {
			var_dump($exception->getMessage());
			exit;
		}
	}

	/**
	 * 初始化参数
	 * @param array $get
	 */
	public function initParams($get) {
		if ($get) {
			$this->_params = $get;
			if (!$get['maxid']) {
				if ($this->_sourceCondition) {
					$condition = ' where ' . $this->_sourceCondition;
				} else {
					$condition = '';
				}
				$sql = "select max($this->_sourceKey) as num from " . $this->_sourceTable . $condition;

				$ds = $this->_dbSource->query($sql);
				$fo = $ds->fetchObject();
				$this->_params['maxid'] = intval($fo->num);
			}
		}
	}

	/**
	 * 获取参数值
	 * @param $key
	 * @return mixed
	 */
	public function getParam($key) {
		return $this->_params[$key];
	}

	public function setParam($key, $value) {
		$this->_params[$key] = $value;
	}

	public function getParams() {
		return $this->_params;
	}

	public function getDbSource() {
		return $this->_dbSource;
	}

	public function getDbAim() {
		return $this->_dbAim;
	}


	public function start($params) {
		$tmp = array();
		foreach ($params as $k => $v) {
			$tmp[] = $k . '=' . $v;
		}
		$param = implode('&', $tmp);
		return '<meta http-equiv="refresh" content="' . $this->_params['interval'] . '; url=' . Env::nquri() . '?' . $param . '" />';
	}

	public function getTable() {
		if ($this->getParam('moduleid') == 23) {
			$table = 'xd_guide';
		} else if ($this->getParam('moduleid') == 26) {
			$table = 'xd_information';
		}else if ($this->getParam('moduleid') == 24) {
			$table = 'xd_quote';
		}
		return $table;
	}


	public function times($info = '', $infoType = 'good') {
		global $MODULE;
		$CATEGORY = PCache::read('category-'.$this->getParam('moduleid').'.php');
		$this->_params['endTime'] = time();
		if ($infoType == 'good') {
			echo '<span style="color:#0f0">';
		} else if ($infoType == 'error') {
			echo '<span style="color:#f00">';
		} else {
			echo '<span style="color:#00f">';
		}
		echo "花费时间 : ".gmstrftime('%H 小时 %M 分 %S 秒', time() - $this->_params['startTime']).'<br>';
		echo "当前处理数据来源于:".$this->getParam('siteName')."<br>";
		echo "目标模块:".$MODULE[$this->getParam('moduleid')]['name']."<br>";
		if ($this->getParam('catid')) {
			echo "目标分类:".$CATEGORY[$this->getParam('catid')]['catname'].'<br>';
		}
		echo "操作数据主表:<b>".$this->getTable().'</b><br>';
		echo "信息总量:".$this->getParam('maxid').'条<br>';
		echo "其他 : {$info}</span>";
	}
	public function end(){
		$title = $this->getParam('siteName');
		send_mail('wanglei@ixdcw.com', $title . '处理完成', '...');
		exit($this->times('处理完成'));
	}
}